package com.cryrabbit.yyystitp.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cryrabbit.yyystitp.R;
import com.cryrabbit.yyystitp.adapters.MyRecycleAdapter;
import com.cryrabbit.yyystitp.model.Tiezi;
import com.cryrabbit.yyystitp.net.RecommmendHttpMethods;
import com.cryrabbit.yyystitp.ui.activities.SendMessageActivity;
import com.cryrabbit.yyystitp.ui.activities.TiezicontentActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;

/**
 * Created by CryRabbit on 2017/5/10.
 */

public class RecommendFragment extends Fragment{
    @BindView(R.id.swipe_refresh_widget)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.fragment_message_list)
    RecyclerView recyclerView;
    @BindView(R.id.fab)
    FloatingActionButton floatingActionButton;
    MyRecycleAdapter myRecycleAdapter;
    List<Tiezi> tiezis;
    RecommmendHttpMethods recommendHttpMethods;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_message,container,false);
        ButterKnife.bind(this,view);
        tiezis=new ArrayList<>();
        tiezis.add(new Tiezi());
        floatingActionButton.setVisibility(View.GONE);
        initList();
        initData();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), SendMessageActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }
    private void initData(){
        recommendHttpMethods=new RecommmendHttpMethods();
        recommendHttpMethods.getNet(new Subscriber<List<Tiezi>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(List<Tiezi> tiezi) {
                Log.i("yyyyy","next");
                tiezis.clear();
                for(int i=0;i<tiezi.size();i++){
                    Log.i("yyyyy",tiezi.get(i).toString());
                    tiezis.add(tiezi.get(i));
                }
                myRecycleAdapter.notifyDataSetChanged();
            }
        },"ysm");
    }
    private void initList(){
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i("ysm","下拉");
                loaddata();
            }
        });
        final LinearLayoutManager layoutManager=new LinearLayoutManager(this.getActivity());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        myRecycleAdapter=new MyRecycleAdapter(tiezis);
        recyclerView.setAdapter(myRecycleAdapter);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem = 0;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == myRecycleAdapter.getItemCount()) {
                    swipeRefreshLayout.setRefreshing(true);
                    Log.i("ysm", "上划");
                    loaddata();
                }
            }
        });
        myRecycleAdapter.setOnItemClickListener(new MyRecycleAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view,Tiezi data) {
                Log.i("ysm",data.toString());
                //添加点击事件,把tiezi当参数传入
                Intent intent = new Intent(getActivity(), TiezicontentActivity.class);
                intent.putExtra("tiezi",data);
                startActivity(intent);

            }
        });
    }
    public void loaddata(){
        swipeRefreshLayout.setRefreshing(false);
    }
}
